home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 335_01 / as1805.y < prev    next >
Text File  |  1990-12-02  |  33KB  |  1,487 lines

  1. %{
  2.  
  3. /*
  4. HEADER:     ;
  5. TITLE:         Frankenstein Cross Assemblers;
  6. VERSION:     2.0;
  7. DESCRIPTION: "    Reconfigurable Cross-assembler producing Intel (TM)
  8.         Hex format object records.  ";
  9. KEYWORDS:     cross-assemblers, 1805, 2650, 6301, 6502, 6805, 6809, 
  10.         6811, tms7000, 8048, 8051, 8096, z8, z80;
  11. SYSTEM:     UNIX, MS-Dos ;
  12. FILENAME:     as1805.y;
  13. WARNINGS:     "This software is in the public domain.  
  14.         Any prior copyright claims are relinquished.  
  15.  
  16.         This software is distributed with no warranty whatever.  
  17.         The author takes no responsibility for the consequences 
  18.         of its use.
  19.  
  20.         Yacc (or Bison) required to compile."  ;
  21. SEE-ALSO:     as1805.doc,frasmain.c;    
  22. AUTHORS:     Mark Zenier;
  23. COMPILERS:     Microport Sys V/AT, ATT Yacc, Turbo C V1.5, Bison (CUG disk 285)
  24.         (previous versions Xenix, Unisoft 68000 Version 7, Sun 3);
  25. */
  26. /* RCA 1802 instruction generation file */
  27. /* November 17, 1990 - character set support */
  28.  
  29. /*
  30.     description    frame work parser description for framework cross
  31.             assemblers
  32.     history        February 2, 1988
  33.             September 11, 1990 - merge table definition
  34.             September 12, 1990 - short file names
  35.             September 14, 1990 - short variable names
  36.             September 17, 1990 - use yylex as external
  37. */
  38. #include <stdio.h>
  39. #include "frasmdat.h"
  40. #include "fragcon.h"
  41.  
  42. #define yylex lexintercept
  43.  
  44. #define CPUMASK        0xc000
  45. #define CPU1802        0x4000    
  46. #define CPU1805        0xc000
  47. #define TS1802PLUS    0x4000    /* mask and match values in table */
  48. #define TS1805    0x8000    /* if select value & mask == mask */
  49. #define ST_INH 0x1
  50. #define ST_IMM 0x2
  51. #define ST_EXP 0x4
  52. #define ST_IO 0x1
  53. #define ST_REG 0x1
  54. #define ST_LDN 0x1
  55. #define ST_RLDI 0x1
  56. #define ST_DBNZ 0x1
  57.     
  58.     int    cpuselect = CPU1805;
  59.     static char    genbdef[] = "[1=];";
  60.     static char    genwdef[] = "[1=]x"; /* x for normal, y for byte rev */
  61.     char ignosyn[] = "[Xinvalid syntax for instruction";
  62.     char ignosel[] = "[Xinvalid operands/illegal instruction for cpu";
  63.  
  64.     long    labelloc;
  65.     static int satsub;
  66.     int    ifstkpt = 0;
  67.     int    fraifskip = FALSE;
  68.  
  69.     struct symel * endsymbol = SYMNULL;
  70.  
  71. %}
  72. %union {
  73.     int    intv;
  74.     long     longv;
  75.     char    *strng;
  76.     struct symel *symb;
  77. }
  78. %token <intv> KOC_BDEF
  79. %token <intv> KOC_ELSE
  80. %token <intv> KOC_END
  81. %token <intv> KOC_ENDI
  82. %token <intv> KOC_EQU
  83. %token <intv> KOC_IF
  84. %token <intv> KOC_INCLUDE
  85. %token <intv> KOC_ORG
  86. %token <intv> KOC_RESM
  87. %token <intv> KOC_SDEF
  88. %token <intv> KOC_SET
  89. %token <intv> KOC_WDEF
  90. %token <intv> KOC_CHSET
  91. %token <intv> KOC_CHDEF
  92. %token <intv> KOC_CHUSE
  93. %token <intv> KOC_opcode
  94. %token <intv> KOC_ioop
  95. %token <intv> KOC_regop
  96. %token <intv> KOC_ldn
  97. %token <intv> KOC_rldi
  98. %token <intv> KOC_dbnz
  99.  
  100. %token <longv> CONSTANT
  101. %token EOL
  102. %token KEOP_AND
  103. %token KEOP_DEFINED
  104. %token KEOP_EQ
  105. %token KEOP_GE
  106. %token KEOP_GT
  107. %token KEOP_HIGH
  108. %token KEOP_LE
  109. %token KEOP_LOW
  110. %token KEOP_LT
  111. %token KEOP_MOD
  112. %token KEOP_MUN
  113. %token KEOP_NE
  114. %token KEOP_NOT
  115. %token KEOP_OR
  116. %token KEOP_SHL
  117. %token KEOP_SHR
  118. %token KEOP_XOR
  119. %token KEOP_locctr
  120. %token <symb> LABEL
  121. %token <strng> STRING
  122. %token <symb> SYMBOL
  123.  
  124. %token KTK_invalid
  125.  
  126. %right    KEOP_HIGH KEOP_LOW
  127. %left    KEOP_OR KEOP_XOR
  128. %left    KEOP_AND
  129. %right    KEOP_NOT
  130. %nonassoc    KEOP_GT KEOP_GE KEOP_LE KEOP_LT KEOP_NE KEOP_EQ
  131. %left    '+' '-'
  132. %left    '*' '/' KEOP_MOD KEOP_SHL KEOP_SHR
  133. %right    KEOP_MUN
  134.  
  135.  
  136. %type <intv> expr exprlist stringlist
  137.  
  138. %start file
  139.  
  140. %%
  141.  
  142. file    :    file allline
  143.     |    allline
  144.     ;
  145.  
  146. allline    :     line EOL
  147.             {
  148.                 clrexpr();
  149.             }
  150.     |    EOL
  151.     |    error EOL
  152.             {
  153.                 clrexpr();
  154.                 yyerrok;
  155.             }
  156.     ;
  157.  
  158. line    :    LABEL KOC_END 
  159.             {
  160.                 endsymbol = $1;
  161.                 nextreadact = Nra_end;
  162.             }
  163.     |          KOC_END 
  164.             {
  165.                 nextreadact = Nra_end;
  166.             }
  167.     |    KOC_INCLUDE STRING
  168.             {
  169.         if(nextfstk >= FILESTKDPTH)
  170.         {
  171.             fraerror("include file nesting limit exceeded");
  172.         }
  173.         else
  174.         {
  175.             infilestk[nextfstk].fnm = savestring($2,strlen($2));
  176.             if( (infilestk[nextfstk].fpt = fopen($2,"r"))
  177.                 ==(FILE *)NULL )
  178.             {
  179.                 fraerror("cannot open include file");
  180.             }
  181.             else
  182.             {
  183.                 nextreadact = Nra_new;
  184.             }
  185.         }
  186.             }
  187.     |    LABEL KOC_EQU expr 
  188.             {
  189.                 if($1 -> seg == SSG_UNDEF)
  190.                 {
  191.                     pevalexpr(0, $3);
  192.                     if(evalr[0].seg == SSG_ABS)
  193.                     {
  194.                         $1 -> seg = SSG_EQU;
  195.                         $1 -> value = evalr[0].value;
  196.                         prtequvalue("C: 0x%lx\n",
  197.                             evalr[0].value);
  198.                     }
  199.                     else
  200.                     {
  201.                         fraerror(
  202.                     "noncomputable expression for EQU");
  203.                     }
  204.                 }
  205.                 else
  206.                 {
  207.                     fraerror(
  208.                 "cannot change symbol value with EQU");
  209.                 }
  210.             }
  211.     |    LABEL KOC_SET expr 
  212.             {
  213.                 if($1 -> seg == SSG_UNDEF
  214.                    || $1 -> seg == SSG_SET)
  215.                 {
  216.                     pevalexpr(0, $3);
  217.                     if(evalr[0].seg == SSG_ABS)
  218.                     {
  219.                         $1 -> seg = SSG_SET;
  220.                         $1 -> value = evalr[0].value;
  221.                         prtequvalue("C: 0x%lx\n",
  222.                             evalr[0].value);
  223.                     }
  224.                     else
  225.                     {
  226.                         fraerror(
  227.                     "noncomputable expression for SET");
  228.                     }
  229.                 }
  230.                 else
  231.                 {
  232.                     fraerror(
  233.                 "cannot change symbol value with SET");
  234.                 }
  235.             }
  236.     |    KOC_IF expr 
  237.             {
  238.         if((++ifstkpt) < IFSTKDEPTH)
  239.         {
  240.             pevalexpr(0, $2);
  241.             if(evalr[0].seg == SSG_ABS)
  242.             {
  243.                 if(evalr[0].value != 0)
  244.                 {
  245.                     elseifstk[ifstkpt] = If_Skip;
  246.                     endifstk[ifstkpt] = If_Active;
  247.                 }
  248.                 else
  249.                 {
  250.                     fraifskip = TRUE;
  251.                     elseifstk[ifstkpt] = If_Active;
  252.                     endifstk[ifstkpt] = If_Active;
  253.                 }
  254.             }
  255.             else
  256.             {
  257.                 fraifskip = TRUE;
  258.                 elseifstk[ifstkpt] = If_Active;
  259.                 endifstk[ifstkpt] = If_Active;
  260.             }
  261.         }
  262.         else
  263.         {
  264.             fraerror("IF stack overflow");
  265.         }
  266.             }
  267.                         
  268.     |    KOC_IF 
  269.             {
  270.         if(fraifskip) 
  271.         {
  272.             if((++ifstkpt) < IFSTKDEPTH)
  273.             {
  274.                     elseifstk[ifstkpt] = If_Skip;
  275.                     endifstk[ifstkpt] = If_Skip;
  276.             }
  277.             else
  278.             {
  279.                 fraerror("IF stack overflow");
  280.             }
  281.         }
  282.         else
  283.         {
  284.             yyerror("syntax error");
  285.             YYERROR;
  286.         }
  287.                 }
  288.                         
  289.     |    KOC_ELSE 
  290.             {
  291.                 switch(elseifstk[ifstkpt])
  292.                 {
  293.                 case If_Active:
  294.                     fraifskip = FALSE;
  295.                     break;
  296.                 
  297.                 case If_Skip:
  298.                     fraifskip = TRUE;
  299.                     break;
  300.                 
  301.                 case If_Err:
  302.                     fraerror("ELSE with no matching if");
  303.                     break;
  304.                 }
  305.             }
  306.  
  307.     |    KOC_ENDI 
  308.             {
  309.                 switch(endifstk[ifstkpt])
  310.                 {
  311.                 case If_Active:
  312.                     fraifskip = FALSE;
  313.                     ifstkpt--;
  314.                     break;
  315.                 
  316.                 case If_Skip:
  317.                     fraifskip = TRUE;
  318.                     ifstkpt--;
  319.                     break;
  320.                 
  321.                 case If_Err:
  322.                     fraerror("ENDI with no matching if");
  323.                     break;
  324.                 }
  325.             }
  326.     |    LABEL KOC_ORG expr 
  327.             {
  328.                 pevalexpr(0, $3);
  329.                 if(evalr[0].seg == SSG_ABS)
  330.                 {
  331.                     locctr = labelloc = evalr[0].value;
  332.                     if($1 -> seg == SSG_UNDEF)
  333.                     {
  334.                         $1 -> seg = SSG_ABS;
  335.                         $1 -> value = labelloc;
  336.                     }
  337.                     else
  338.                         fraerror(
  339.                         "multiple definition of label");
  340.                     prtequvalue("C: 0x%lx\n",
  341.                         evalr[0].value);
  342.                 }
  343.                 else
  344.                 {
  345.                     fraerror(
  346.                      "noncomputable expression for ORG");
  347.                 }
  348.             }
  349.     |          KOC_ORG expr 
  350.             {
  351.                 pevalexpr(0, $2);
  352.                 if(evalr[0].seg == SSG_ABS)
  353.                 {
  354.                     locctr = labelloc = evalr[0].value;
  355.                     prtequvalue("C: 0x%lx\n",
  356.                         evalr[0].value);
  357.                 }
  358.                 else
  359.                 {
  360.                     fraerror(
  361.                      "noncomputable expression for ORG");
  362.                 }
  363.             }
  364.     |    LABEL KOC_CHSET
  365.             {
  366.                 if($1 -> seg == SSG_UNDEF)
  367.                 {
  368.                     $1 -> seg = SSG_EQU;
  369.                     if( ($1->value = chtcreate()) <= 0)
  370.                     {
  371.         fraerror( "cannot create character translation table");
  372.                     }
  373.                     prtequvalue("C: 0x%lx\n", $1 -> value);
  374.                 }
  375.                 else
  376.                 {
  377.             fraerror( "multiple definition of label");
  378.                 }
  379.             }
  380.     |        KOC_CHUSE
  381.             {
  382.                 chtcpoint = (int *) NULL;
  383.                 prtequvalue("C: 0x%lx\n", 0L);
  384.             }
  385.     |        KOC_CHUSE expr
  386.             {
  387.                 pevalexpr(0, $2);
  388.                 if( evalr[0].seg == SSG_ABS)
  389.                 {
  390.                     if( evalr[0].value == 0)
  391.                     {
  392.                         chtcpoint = (int *)NULL;
  393.                         prtequvalue("C: 0x%lx\n", 0L);
  394.                     }
  395.                     else if(evalr[0].value < chtnxalph)
  396.                     {
  397.                 chtcpoint = chtatab[evalr[0].value];
  398.                 prtequvalue("C: 0x%lx\n", evalr[0].value);
  399.                     }
  400.                     else
  401.                     {
  402.             fraerror("nonexistent character translation table");
  403.                     }
  404.                 }
  405.                 else
  406.                 {
  407.                     fraerror("noncomputable expression");
  408.                 }
  409.             }
  410.     |        KOC_CHDEF STRING ',' exprlist
  411.             {
  412.         int findrv, numret, *cha